const modules = import.meta.glob("../**/**/*.vue");
import website from "@/config/website.js";

let RouterPlugin = function () {
    this.$router = null;
    this.$store = null;
};

/**
 * 格式化路径
 * @param menuPath 菜单路径
 * @returns {*|string}
 */
function formatMenuPath(menuPath) {
    return menuPath.startsWith("/") ? menuPath : '/' + menuPath
}

RouterPlugin.install = function (option = {}) {
    this.$router = option.router;
    this.$store = option.store;
    this.$router.$quickRouter = {
        //全局配置
        routerList: [],
        safe: this,
        // 设置标题
        setTitle: (title) => {
            const defaultTitle = website.title;
            title = title ? `${defaultTitle}-${title}` : defaultTitle;
            document.title = title;
        },
        //处理路由
        getPath: function (params) {
            let {src} = params;
            return src || "/";
        },
        //设置路由值
        getValue: function (route) {
            let value = "";
            if (route.query.src) {
                value = route.query.src;
            } else {
                value = route.path;
            }

            return value;
        },
        //动态路由
        formatRoutes(menuRouter = [], first) {
            if (menuRouter.length === 0) return;
            const aRouter = menuRouter.map((menu) => {
                const menuName = menu[website.menuDefaultProps.label];
                const menuPath = menu[website.menuDefaultProps.path];
                const menuIcon = menu[website.menuDefaultProps.icon];
                const menuChildren = menu[website.menuDefaultProps.children];
                const layoutModule = modules["../page/index/index.vue"]
                const emptyModule = modules["../page/index/empty.vue"]
                const viewModule = modules[`../views${formatMenuPath(menuPath)}.vue`]
                if (this.routerList.includes(menuPath)) return null;
                const isChild = menuChildren && menuChildren.length !== 0;
                return {
                    path: formatMenuPath(menuPath),
                    component: first ? layoutModule : isChild ? emptyModule : viewModule,
                    name: menuName,
                    icon: menuIcon,
                    meta: {
                        keepAlive: menu.keepAlive,
                        icon: menuIcon,
                        type: menu.type,
                    },
                    children: !isChild ? first ? [
                        {
                            component: viewModule,
                            icon: menuIcon,
                            name: menuName,
                            path: formatMenuPath(menuPath),
                            meta: {
                                keepAlive: menu.keepAlive,
                                icon: menuIcon,
                                type: menu.type,
                            },
                        },] : [] : this.formatRoutes(menuChildren, false),
                };
            })
            if (first) {
                const firstRouterPath = aRouter.length > 0 && aRouter[0][website.menuDefaultProps.path];
                if (firstRouterPath && !this.routerList.includes(firstRouterPath)) {
                    aRouter.forEach((item) => {
                        this.safe.$router.addRoute(item);
                    });
                    this.routerList.push(firstRouterPath);
                }
            } else {
                return aRouter;
            }
        },
    };
};
export default RouterPlugin;
